import requests
import threading
import time

URL = 'http://localhost:8080/'
THREADS = 50  # 并发线程数
REQUESTS_PER_THREAD = 20  # 每个线程请求次数

def worker(thread_id, results):
    success = 0
    for _ in range(REQUESTS_PER_THREAD):
        try:
            response = requests.get(URL, timeout=2)
            if response.status_code == 200 and response.text == 'Hello, World!':
                success += 1
        except Exception as e:
            pass
    results[thread_id] = success

def main():
    threads = []
    results = [0] * THREADS
    start = time.time()
    for i in range(THREADS):
        t = threading.Thread(target=worker, args=(i, results))
        threads.append(t)
        t.start()
    for t in threads:
        t.join()
    end = time.time()
    total_requests = THREADS * REQUESTS_PER_THREAD
    total_success = sum(results)
    print(f"总请求数: {total_requests}")
    print(f"成功响应数: {total_success}")
    print(f"耗时: {end - start:.2f} 秒")
    print(f"QPS: {total_success / (end - start):.2f}")

if __name__ == '__main__':
    main() 